package com.example.quanxin.controller;/*
 *
 *   welcome:11311
 *   创建时间:2023/5/20
 *
 */


import com.example.quanxin.config.GlobalConfig;
import com.example.quanxin.entity.*;
import com.example.quanxin.service.ApiService;
import com.example.quanxin.service.ApiStudentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
//@CrossOrigin(value = "/student")
@RequestMapping("/student")
@Tag(name = "学生个人档案页面接口")
public class ApiStudentController {

    @Resource
    private ApiStudentService apiStudentService;


    @Resource
    private  ApiService apiService;

    @GetMapping("/login")
    public ReturnT<StudentInfo> login(@RequestParam("name") String name) throws IOException {
        Map<String, String> stringStringMap = GlobalConfig.getStudentMapping();
        try {
            String s = stringStringMap.get(name);
            String uname=  s.split(",")[0];
            String password=  s.split(",")[1];
            LoginParam loginParam=new LoginParam();
            loginParam.setUsername(uname);
            loginParam.setPassword(password);
            apiStudentService.getAndStoreCookies(loginParam);
            StudentInfo studentInfo=new StudentInfo();
            studentInfo.setUserName(name);
            studentInfo.setAge(Integer.valueOf(s.split(",")[2]));
            studentInfo.setSex(s.split(",")[3]);
            studentInfo.setSubject(s.split(",")[4]);
            studentInfo.setClassNum(s.split(",")[5]);
            studentInfo.setCpi(Long.valueOf(s.split(",")[6]));
            studentInfo.setUid(Long.valueOf(s.split(",")[7]));
            studentInfo.setClassId(Long.valueOf(s.split(",")[8]));
            studentInfo.setImgUrl(s.split(",")[9]);
            studentInfo.setUName(uname);
            loginParam.setPassword(password);
            return ReturnT.ok(studentInfo);

        }catch (Exception e){
            String s = stringStringMap.get("谭春勇");
            String uname=  s.split(",")[0];
            String password=  s.split(",")[1];
            LoginParam loginParam=new LoginParam();
            loginParam.setUsername(uname);
            loginParam.setPassword(password);
            apiStudentService.getAndStoreCookies(loginParam);
            StudentInfo studentInfo=new StudentInfo();
            studentInfo.setUserName(name);
            studentInfo.setAge(Integer.valueOf(s.split(",")[2]));
            studentInfo.setSex(s.split(",")[3]);
            studentInfo.setSubject(s.split(",")[4]);
            studentInfo.setClassNum(s.split(",")[5]);
            studentInfo.setCpi(Long.valueOf(s.split(",")[6]));
            studentInfo.setUid(Long.valueOf(s.split(",")[7]));
            studentInfo.setClassId(Long.valueOf(s.split(",")[8]));
            studentInfo.setImgUrl(s.split(",")[9]);
            studentInfo.setUName(uname);
            loginParam.setPassword(password);
            return ReturnT.ok(studentInfo);
        }

    }

    @GetMapping("/getClassScore")
    @Operation(summary ="获取任务成绩折线图")
    public ReturnT<ClassScoreAvg> getClassScore(@RequestParam("courseId")  @Parameter(description="课程id")Long courseId,
                                 @RequestParam("classId") @Parameter(description="班级id") Long classId,
                                 @RequestParam("cpi") @Parameter(description="学习id") Long cpi
//                                 @RequestParam("personId") @Parameter(description="人员id") Long personId
    ) throws Exception {
        ClassScoreAvg classScore = apiService.getClassScore(courseId, classId);
        classScore=apiStudentService.getClassScore(courseId, classId,cpi,cpi,classScore);
        return ReturnT.ok(classScore);
    }


    @GetMapping("/getClassSchedule")
    @Operation(summary ="获取任务完成程度")
    public ReturnT<List<StudentChapter>> getClassSchedule(@RequestParam("courseId")  @Parameter(description="课程id")Long courseId,
                                                @RequestParam("classId") @Parameter(description="班级id") Long classId,
                                                @RequestParam("cpi") @Parameter(description="学习id") Long cpi,
                                                @RequestParam("name") @Parameter(description="姓名")String  name
                                                ) throws Exception {
        Map<String, String> stringStringMap = GlobalConfig.getStudentMapping();
        String uname=null;
        String password=null;
        try {
            String s = stringStringMap.get(name);
            uname=  s.split(",")[0];
            password=  s.split(",")[1];
        }catch (Exception e){
            String s = stringStringMap.get("谭春勇");
             uname=  s.split(",")[0];
            password=  s.split(",")[1];
        }

        Map<Long, String> courseDate = GlobalConfig.getCourseDate();
        String pEnc=null;
        Long courseCpi=null;
        try {
            pEnc=courseDate.get(courseId).split(",")[0];
            courseCpi= Long.valueOf(courseDate.get(courseId).split(",")[2]);
        }
        catch (Exception e){
            pEnc=courseDate.get(243185166l).split(",")[0];
            courseCpi= Long.valueOf(courseDate.get(243185166l).split(",")[2]);
        }
        List<StudentChapter> classSchedule = apiStudentService.getClassSchedule(courseId, classId, cpi,pEnc,uname,password,courseCpi);
        return ReturnT.ok(classSchedule);
    }


    @GetMapping("/getClassJob")
    @Operation(summary ="获取课程完成情况")
    public ReturnT<ClassJob> getClassJob(@RequestParam("courseId")  @Parameter(description="课程id")Long courseId,
                                                          @RequestParam("classId") @Parameter(description="班级id") Long classId,
                                                          @RequestParam("cpi") @Parameter(description="学习id") Long cpi
    ) throws Exception {
        Map<Long, String> courseDate = GlobalConfig.getCourseDate();
        String pEnc=null;
        Long courseCpi=null;
        try {
            pEnc=courseDate.get(courseId).split(",")[1];
            courseCpi= Long.valueOf(courseDate.get(courseId).split(",")[2]);
        }
        catch (Exception e){
            pEnc=courseDate.get(243191609l).split(",")[1];
            courseCpi= Long.valueOf(courseDate.get(243185166l).split(",")[2]);
        }
        ClassJob classJob = apiStudentService.getClassJob(courseId, classId, cpi,pEnc,courseCpi);
        return ReturnT.ok(classJob);
    }

    @GetMapping("/getStudentClassSchedule")
    @Operation(summary ="获取学生个人课程进度")
    public ReturnT<List<StudentClassSchedule>> getStudentClassSchedule(@RequestParam("courseId")  @Parameter(description="课程id")Long courseId,
                                         @RequestParam("classId") @Parameter(description="班级id") Long classId,
                                         @RequestParam("cpi") @Parameter(description="学习id") Long cpi
    ) throws Exception {
        Map<Long, String> courseDate = GlobalConfig.getCourseDate();
        String pEnc=null;
        try {
            pEnc=courseDate.get(courseId).split(",")[0];
        }
        catch (Exception e){
            pEnc=courseDate.get(243185166l).split(",")[0];
        }
//        String pEnc=courseDate.get(courseId).split(",")[0];
        List<StudentClassSchedule> studentClassSchedule = apiStudentService.getStudentClassSchedule(courseId, classId, cpi, pEnc);
        return ReturnT.ok(studentClassSchedule);
    }

    @GetMapping("/getAiCourseDate")
    @Operation(summary ="AI学习计划")
    public ReturnT<AiCourseDate> getAiCourseDate(@RequestParam("courseId")  @Parameter(description="课程id")Long courseId){
        Map<Long, String> courseDate = GlobalConfig.getAiCourseDate();
        AiCourseDate aiCourseDate = new AiCourseDate();

        try {
            String s = courseDate.get(courseId);
            aiCourseDate.setTarget(s.split("&")[0]);
            aiCourseDate.setPlan(s.split("&")[1]);
        }catch (Exception e ){
            String s = courseDate.get(243185166l);
            aiCourseDate.setTarget(s.split("&")[0]);
            aiCourseDate.setPlan(s.split("&")[1]);
        }
        return ReturnT.ok(aiCourseDate);
    }

    @GetMapping("/getClassScoreSummary")
    @Operation(summary ="课程任务完成情况汇总")
    public ReturnT<ClassScoreSummary> getClassScoreSummary(@RequestParam("courseId")  @Parameter(description="课程id")Long courseId,
                                                @RequestParam("classId") @Parameter(description="班级id") Long classId,
                                                @RequestParam("cpi") @Parameter(description="学习id") Long cpi
//                                 @RequestParam("personId") @Parameter(description="人员id") Long personId
    ) throws Exception {
         ClassScoreSummary classScoreSummary = apiStudentService.getClassScoreSummary(courseId, classId, cpi, cpi);
        return ReturnT.ok(classScoreSummary);
    }

}
